package com.gerald.shop.controller;


import com.gerald.shop.common.utils.PageUtils;
import com.gerald.shop.common.utils.R;
import com.gerald.shop.entity.GoodsEntity;
import com.gerald.shop.fegin.api.GoodsService;
import com.gerald.shop.vo.GoodsVo;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Gerald 2022/1/3 22:22
 * description:
 */
@RestController
@RequestMapping("/system/goods")
@Slf4j
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @GetMapping("/list")
    public R list() {
        return R.ok().put("data", goodsService.list());
    }


    @GetMapping("/page")
    public R page(@RequestParam Map<String, Object> params) {
        PageUtils page = goodsService.page(params);
        List<?> list = page.getList();
        List<GoodsVo> goodsVoList = new ArrayList<>();
        for (Object o : list) {
            //o是LinkdHashMap
            GoodsVo goodsVo = new GoodsVo();

            Map map = (Map) o;
            System.out.println(map);
            //把map拷贝到vo中
//            BeanUtils.populate(goodsVo, map);

        }

        return R.ok().put("page", page);
    }

    /**
     * @author Gerald 2022/1/7 18:57
     * description:商品添加
     */
    @PostMapping("/save")
    public R save(@RequestBody GoodsVo goodsVo) {
//        log.debug("goodsEntity",goodsEntity);
        GoodsEntity entity = new GoodsEntity();

        //2 把vo中的属性拷贝到entity
        BeanUtils.copyProperties(goodsVo, entity);

        //把vo中的图片数组全部放到Entity中（）
        List<Map<String, String>> uploadPngArray = goodsVo.getUploadPngArray();
        if (uploadPngArray != null && uploadPngArray.size() > 0) {
            //1 创建一个人buffer来组合图片地址
            StringBuffer buffer = new StringBuffer();
            for (Map<String, String> map : uploadPngArray) {

                //图片路径的拼接
                String path = map.get("fpath");
                buffer.append(path).append(",");
            }
            //找到最后一个“，”替换掉
            String gpntPath = buffer.substring(0, buffer.length() - 1);
            entity.setGpng(gpntPath);
        }
//        boolean save = goodsService.save(goodsEntity);

        Boolean save = goodsService.save(entity);
        return R.write(true);
    }

    /**
     * @author Gerald 2022/1/7 19:25
     * description:修改
     */
    @PostMapping("/update")
    public R update(@RequestBody GoodsEntity goodsEntity) {
        log.debug("goodsEntity", goodsEntity);
        boolean save = goodsService.update(goodsEntity);
        return R.write(save);
    }

    /**
     * @author Gerald 2022/1/7 19:30
     * description:查询
     */
    @PostMapping("/info/{id}")
    public R info(@PathVariable long id) {
        log.debug("id:{}", id);
        GoodsEntity info = goodsService.info(id);
        return R.ok().put("data", info);
    }


    /**
     * @author Gerald 2022/1/7 19:30
     * description:删除，批量删除
     */
    @PostMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        log.debug("ids:{}", ids);
        boolean delete = goodsService.delete(ids);
        return R.write(delete);
    }

}
